package com.hefei.blog.core.lucene.article;

import java.util.Calendar;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.util.BytesRef;

import com.hefei.blog.base.utils.HTMLParser;
import com.hefei.blog.base.utils.html.FileHtmlUtils;
import com.hefei.blog.core.data.Post;

public class ArticleDocument {

	public static Document genDocument(Post post) {
		Document doc = new Document();
		
		Field idField = new StringField("id", String.valueOf(post.getId()), Field.Store.YES);
		doc.add(idField);
		Field idSortFiled = new SortedDocValuesField("id",  new BytesRef( String.valueOf(post.getId())) );
		doc.add(idSortFiled);
		
		Field titleField = new StringField("title", post.getTitle(), Field.Store.YES);
		doc.add(titleField);
		Field summaryField = new StringField("summary", post.getSummary(), Field.Store.YES);
		doc.add(summaryField);
		
		String plainContent = HTMLParser.getText(FileHtmlUtils.read(post.getFilepath()));
		Field contentField = new TextField("content", plainContent, Field.Store.NO);
		doc.add(contentField);
		Field pathField = new StringField("filepath", post.getFilepath(), Field.Store.YES);
		doc.add(pathField);
		Field tagsField = new StringField("tags", post.getTags(), Field.Store.YES);
		doc.add(tagsField);
		Field privacyField = new StringField("privacy", String.valueOf(post.getPrivacy()), Field.Store.YES); //new IntPoint("privacy", post.getPrivacy());// 私密
		doc.add(privacyField);
		Field featuredField = new StringField("featured", String.valueOf(post.getFeatured()), Field.Store.YES);//IntPoint("featured", post.getPrivacy());// 推荐状态
		doc.add(featuredField);
		Field favorsField = new StringField("favors", String.valueOf(post.getFavors()), Field.Store.YES);//IntPoint("favors", post.getPrivacy());// 喜欢数
		doc.add(favorsField);
		Field commentsField = new StringField("comments", String.valueOf(post.getComments()), Field.Store.YES);// new IntPoint("comments", post.getPrivacy());// 评论数
		doc.add(commentsField);
		Field viewsField = new StringField("views", String.valueOf(post.getViews()), Field.Store.YES);//IntPoint("views", post.getPrivacy());// 阅读数
		doc.add(viewsField);
		Field statusField = new StringField("status", String.valueOf(post.getStatus()), Field.Store.YES);//Inew IntPoint("status", post.getPrivacy());// 文章状态
		doc.add(statusField);
	
		Field authorIdField = new StringField("authorId", String.valueOf(post.getAuthorId()), Field.Store.YES);
		doc.add(authorIdField);
		
		Field createTimeField = new StringField("createTime", String.valueOf(post.getCreated().getTime()), Field.Store.YES);
		doc.add(createTimeField);
		Field createTimeSortFiled = new SortedDocValuesField("createTime",  new BytesRef( String.valueOf(post.getCreated().getTime())) );
		doc.add(createTimeSortFiled);
		return doc;
	}
	
	public static Post parseDocument(Document doc){
		Post post = new Post();
		post.setId( Long.valueOf(doc.get("id")));
		post.setTitle(doc.get("title"));
		post.setSummary(doc.get("summary"));
		post.setFilepath(doc.get("filepath"));
		post.setTags(doc.get("tags"));
		post.setPrivacy(Integer.valueOf(doc.get("privacy")));
		post.setFeatured(Integer.valueOf(doc.get("featured")));
		post.setFavors(Integer.valueOf(doc.get("favors")));
		post.setComments(Integer.valueOf(doc.get("comments")));
		post.setViews(Integer.valueOf(doc.get("views")));
		post.setStatus(Integer.valueOf(doc.get("status")));
		post.setAuthorId(Long.valueOf(doc.get("authorId")));
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(Long.valueOf(doc.get("createTime")));
		post.setCreated( c.getTime() );
		
		return post;
	}
}
